var car_series_id = 0; //视频id  !注意
var path_name="car_video_broadcast";
$(document).ready(function() {
	/* 根据token获取用户信息 */
		get_user_mess_and_token();
			//自己代码
		/* http://127.0.0.1:8899/html/car_video_broadcast.html?1    */
		var url = location.search;
		car_series_id = url.substr(1); //视频id
		//加载视频方法
		loading_video_broadcas();
		//视频侧边栏
		video_ce_bian();
		//相关车系
		interrelated_series();
		//最新推荐
		load_all_car_video_data();
		tite_li(car_series_id);
		
		

});

//加载视频方法——标题 -发布时间 -用户
function loading_video_broadcas(){
	var count="";
	$.get(path+"VideoController/select_videoByID_video/"+car_series_id,function(data){
		
		if (data.video_broadcast_count < 10000) {
			count = data.video_broadcast_count;
		} else if (data.video_broadcast_count < 100000000) {
			count = (Math.floor((data.video_broadcast_count / 10000) * 100) / 100) + "万"  ;
		}
		//视频详情部分
		str="<h1 class='jsx-2170876313  tw-text-24 xl:tw-text-32  tw-text-common-black tw-text-semibold tw-mb-12'>"+data.video_introduction+"</h1>" +
			"<div class='jsx-2170876313 tw-text-color-gray-800 tw-mb-12 tw-flex tw-items-center tw-text-16'>"+
				"<span class='jsx-2170876313 tw-mr-24'>发布于<!-- -->"+data.video_upload_date+"</span>"+
				"<span class='jsx-2170876313 font-RPP4MYtWOvsBmV80'>"+count+"<!-- -->次播放</span>"+
			"</div>"+	
			"<div class='jsx-2170876313 line-1 tw-text-16 max-to-right-104  tw-text-color-gray-900'>"+data.video_introduction+"</div>";
			
		//用户部分
		Userstr="<a href='http://127.0.0.1:8899/html/car_my_homepage.html?"+data.user.user_id+"' target=\"_blank\" class=\"jsx-2348083694 tw-relative tw-mr-8 tw-flex-shrink-0 tw-flex-grow-0\" rel=\"opener\">"+
					"<div style=\"display:inline-block;max-width:100%;overflow:hidden;position:relative;z-index:0\" class=\"tw-w-40 tw-h-40 tw-bg-cover tw-bg-center tw-rounded-full\">"+
						"<div style=\"box-sizing:border-box;display:block;max-width:100%\">"+
							"<img style=\"max-width:100%;display:block;margin:0;border:none;padding:0\" alt=\"\" src=\"data:image/svg+xml;utf-8,<svg width=&quot;300&quot; height=&quot;300&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; version=&quot;1.1&quot;/>\" aria-hidden=\"true\" role=\"presentation\">"+
						"</div>"+
						"<img src="+imagePath+data.user.user_head_portrait_url+" alt=\""+data.user.user_name+"头像\" width=\"300\" height=\"300\" style=\"position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%;object-fit:cover\" class=\"\">"+
					"</div>"+
					"<img src=\"//lf3-motor.dcarstatic.com/obj/motor-fe-static/motor/pc/_next/./static/images/yellow-v-2b6c52a00bffaa8dec8c..png\" class=\"jsx-2348083694 tw-absolute tw-right-0 tw-bottom-0 tw-h-12 tw-w-12\">"+
				"</a>"+
				"<div class=\"jsx-2348083694 tw-mr-32\">"+
					"<div class=\"jsx-2348083694 tw-text-common-black tw-text-16 tw-mb-2\">"+
						"<a href='http://127.0.0.1:8899/html/car_my_homepage.html?"+data.user.user_id+"' target=\"_blank\" class=\"jsx-2348083694\" rel=\"opener\">"+data.user.user_name+"</a>"+
					"</div>"+
					"<div class=\"jsx-2348083694 tw-text-common-gray tw-text-14\">"+
						"<span class=\"jsx-2348083694 tw-mr-8\">关注<!-- -->32<!-- -->人</span>"+
						"<span class=\"jsx-2348083694\">274万<!-- -->粉丝</span>"+
					"</div>"+
				"</div>"+
				"<button type='button' class='focus-btn tw-flex tw-items-center tw-justify-center tw-rounded-4 button new_primary button_root__3V6Iu button_new_primary__2mZuy' onclick='insert_or_delete_follow_user("+data.user.user_id+")' id='follow' >关注</button>"+
				"<button type='button' class='focus-btn tw-flex tw-items-center tw-justify-center tw-rounded-4 button new_primary button_root__3V6Iu button_new_primary__2mZuy' onclick='insert_or_delete_follow_user("+data.user.user_id+")' style='display: none;' id='followed'>已关注</button>"
				
		
		$("embed").attr("src","http://120.48.47.144:8899/videos/"+data.video_url);
		$("#user_guan_zhu").children().remove();
		$("#user_guan_zhu").append(Userstr);
		$("#video_xianqing").children().remove();  
		$("#video_xianqing").append(str);
		if(user_id!=0){
			select_user_whether_follow_user(data.user.user_id);//关注用户
			isno();//收藏视频
		}
	},"json")
}

//视频侧边栏
function video_ce_bian(){
	$.get(path+"VideoController/select_video_id_all/"+car_series_id,function(data){
		var str="";
		var count="";
		$(data).each(function(index,item){
			if (item.video_broadcast_count < 10000) {
				count = item.video_broadcast_count;
			} else if (item.video_broadcast_count < 100000000) {
				count = (Math.floor((item.video_broadcast_count / 10000) * 100) / 100) + "万";
			}
			str+="<li class=\"jsx-1564469232\">"+
					"<div class=\"jsx-2348083694 jsx-3682714230 video-card tw-w-full  tw-flex tw-py-14   tw-h-100 xl:tw-h-126 series-card tw-pl-16 tw-pr-16   tw-px-6\">"+
						"<a target=\"_self\" class=\"jsx-2348083694 jsx-3682714230 tw-flex-shrink-0 tw-flex-grow-0\" href='http://127.0.0.1:8899/html/car_video_broadcast.html?"+item.video_id+"' rel=\"opener\">"+
							"<div class=\"jsx-2348083694 jsx-3682714230 tw-rounded-2 tw-relative  tw-h-full  tw-overflow-hidden tw-w-108 xl:tw-w-132  tw-h-72 xl:tw-h-88 tw-mr-10\">"+
								"<div class=\"cover-image g-load-img-wrap tw-object-cover tw-object-center \" style=\"display: block; overflow: hidden; position: absolute; inset: 0px; z-index: 0;\">"+
									"<img src='"+imagePath+item.video_images_url+"'"+
									"alt="+item.video_introduction+" width=\"133\""+
									"height=\"88\" class=\"zoom\""+
									"style=\"position: absolute; inset: 0px; box-sizing: border-box; padding: 0px; border: none; margin: auto; display: block; width: 0px; height: 0px; min-width: 100%; max-width: 100%; min-height: 100%; max-height: 100%; object-fit: cover;\">"+
								"</div>"+
								"<div class=\"time-duration_duration__2iTkq\">"+
									"<div class=\"time-duration_time__2HNys\">"+
										"<span>"+item.video_duration+"</span>"+
									"</div>"+
								"</div>"+		
							"</div>"+
						"</a>"+				
						"<div class=\"jsx-2348083694 jsx-3682714230 tw-flex tw-justify-between tw-items-stretch tw-flex-col tw-flex-shrink tw-flex-grow-0 tw-min-w-0 tw-h-72 xl:tw-h-88\">"+
							"<div class=\"jsx-2348083694 jsx-3682714230 line-2 tw-text-14 xl:tw-text-16  tw-text-common-cyan-blue\">"+
								"<a target=\"_self\" href='http://127.0.0.1:8899/html/car_video_broadcast.html?"+item.video_id+"' class=\"jsx-2348083694 jsx-3682714230\" rel=\"opener\">"+item.video_introduction+"</a>"+
							"</div>"+	
							"<div class=\"jsx-2348083694 jsx-3682714230 tw-whitespace-nowrap tw-flex tw-min-w-0 tw-justify-between tw-text-color-gray-800\">"+		
								"<span class=\"jsx-2348083694 jsx-3682714230 tw-text-14 tw-inline-block tw-flex-shrink-0 tw-flex-grow-0 tw-text-common-gray\">"+		
									"<span class=\"jsx-2348083694 jsx-3682714230\">"+count+"次播放</span>"+
								"</span>"+
							"</div>"+
						"</div>"+
					"</div>"+
				"</li>"		
		})
		$("#video_ce_ban li").remove();
		$("#video_ce_ban").append(str);
	},"json")
}

//相关车系
function interrelated_series(){
	$.get(path+"Car_seriesController/select_interrelated_series/"+car_series_id,function(data){
		var str="";
		$(data).each(function(index,item){
			if(index==0||index==3||index==6){
				str+="<div class=\"swiper-wrapper\" style=\"transform: translate3d(0px, 0px, 0px);\">";
			}
			if(index>8){
				str+="";
			}else{
			str+="<div class=\"swiper-slide wrap swiper-slide-active\" style=\"margin-right: 6px;\">"+
					"<div class=\"series-card_root__iv-TD\">"+
						"<div class=\"series-card_header__3kwzI\">"+
							"<p class=\"series-card_brand__16lxB\">"+
								"<a class=\"series-card_brand-link__1WuOK common-link_root__2VBT4\" target=\"_blank\" href='' rel=\"opener\">"+
									"<img class=\"series-card_brand-logo__B6FDB\""+
										"src='"+imagePath+item.brand.brand_images.brand_images_car_logo_url+"'"+
										"alt="+item.brand.brand_name+">"+
								"</a>"+
								"<a class=\"series-card_series-name__3SvjA common-link_root__2VBT4\""+
									"target=\"_blank\" href='http://127.0.0.1:8899/html/car_series_index.html?"+item.car_series_id+"'"+
									"title='"+item.car_series_name+"' rel=\"opener\">"+item.car_series_name+"</a>"+
							"</p>"+
							"<a class=\"series-card_score__37ghA common-link_root__2VBT4\""+
								"style=\"background:7px center / 35px auto no-repeat url(//lf3-motor.dcarstatic.com/obj/motor-fe-static/motor/pc/_next/./static/images/dong-che-fen-logo-918ecfac26a0880100c4..png), linear-gradient(to right, #000 0%, #000 60px, #e6e8f2 60px)\""+
								"target=\"_blank\" href='#'"+
								"rel=\"opener\">"+
								"<span class=\"series-card_score-number__3F6Mo\">"+item.car_points.car_points_avg+"</span>分"+
							"</a>"+
						"</div>"+
						"<div class=\"series-card_info__AhzCM\">"+
							"<a class=\"common-link_root__2VBT4\" style=\"font-size:0\" target=\"_blank\" href='http://127.0.0.1:8899/html/car_series_index.html?"+item.car_series_id+"' rel=\"opener\">"+
								"<div style=\"display:inline-block;max-width:100%;overflow:hidden;position:relative;z-index:0\">"+
									"<div style=\"box-sizing:border-box;display:block;max-width:100%\">"+
										"<img style=\"max-width:100%;display:block;margin:0;border:none;padding:0\""+
											"src=\"data:image/svg+xml;utf-8,<svg width=&quot;90&quot; height=&quot;56&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; version=&quot;1.1&quot;/>\""+
											"aria-hidden=\"true\" role=\"presentation\">"+
									"</div>"+	
									"<img src='"+imagePath+item.car_series_images_url_1+"'"+
										"alt='' width=\"90\" height=\"56\""+
										"style=\"position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%;object-fit:cover\""+
										"class=\"\">"+
								"</div>"+
							"</a>"+
							"<a class=\"common-link_root__2VBT4\" target=\"_blank\" href='http://127.0.0.1:8899/html/car_series_index.html?"+item.car_series_id+"' rel=\"opener\">"+
								"<table class=\"series-card_price__1HDqw\">"+
									"<tr class=\"series-card_price-item__Ld2ed\">"+
										"<td class=\"series-card_price-label__2jZFj\">最低售价：</td>"+
										"<td class=\"series-card_price-dealer__O2Cdu font-hYThLvBRwlPwxuiC\">"+item.min_manufacturer_price+"万</td>"+
									"</tr>"+
									"<tr class=\"series-card_price-item__Ld2ed\">"+
										"<td class=\"series-card_price-label__2jZFj\">最高降价：</td>"+
										"<td class=\"series-card_price-cut__1MU-X font-hYThLvBRwlPwxuiC\">"+
											"<i class=\"DCD_Icon icon_reduce series-card_price-cut-icon__33PzZ\"></i>"+item.car_model.car_model_max_decline+"万"+
										"</td>"+
									"</tr>"+
								"</table>"+
							"</a>"+	
							"<a class=\"series-card_dealer-button__T-Jpr common-link_root__2VBT4\" target=\"_blank\" href='http://127.0.0.1:8899/html/car_purchase_calculation.html?"+item.car_series_id+"/0' rel=\"opener nofollow\">购车计算</a>"+
						"</div>"+
						"<div class=\"series-card_footer__3myC2\">"+
							"<a class=\"series-card_footer-link__3JuCQ common-link_root__2VBT4\""+
								"target=\"_blank\" href='http://127.0.0.1:8899/html/car_series_article.html?"+item.car_series_id+"' "+
								"rel=\"opener\">资讯</a>"+
							"<span style=\"display:inline-block;width:1px;height:12px;background:#E6E8F2\"></span>"+
							"<a class=\"series-card_footer-link__3JuCQ common-link_root__2VBT4\""+
								"target=\"_blank\" href='http://127.0.0.1:8899/html/car_actual_large_picture.html?"  + item.brand.brand_id + "/"+item.car_series_id+"/0/0' rel=\"opener\">图片</a>"+
						"</div>"+
					"</div>"+	
				"</div>"
			}
			if(index==2||index==5||index==8){
				str+="</div>";
			}
		});
		$("#interrelated div").remove();
		$("#interrelated").append(str);
	},"json");
}
//最新推荐
function load_all_car_video_data(){
	$.get(path+"PublicController/load_all_car_video_data",function(data){
		var str="";
		$(data.all_new_video_mess).each(function(index,item){
			if(index>3){
				str+=""
			}else{
			str+="<li class=\"jsx-2462430676 tw-relative tw-z-10 border\">"+
					"<div style=\"border-bottom:1px #f2f4fa solid\" class=\"jsx-2348083694 jsx-3258712038 video-card tw-w-full  tw-flex tw-py-14   tw-h-100 xl:tw-h-126 undefined tw-bg-white tw-px-16\">"+
						"<a target=\"_self\" class=\"jsx-2348083694 jsx-3258712038 tw-flex-shrink-0 tw-flex-grow-0\" href='http://127.0.0.1:8899/html/car_video_broadcast.html?"+item.video_id+"' rel=\"opener\">"+
							"<div class=\"jsx-2348083694 jsx-3258712038 tw-rounded-2 tw-relative  tw-h-full  tw-overflow-hidden tw-w-108 xl:tw-w-132  tw-h-72 xl:tw-h-88 tw-mr-10\">"+
								"<div style=\"display:block;overflow:hidden;position:absolute;top:0;left:0;bottom:0;right:0;z-index:0\" class=\"cover-image g-load-img-wrap tw-object-cover tw-object-center \">"+
									"<img src='"+imagePath+item.video_images_url+"' alt='"+item.video_introduction+"' width=\"133\" height=\"88\""+
										"style=\"position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%;object-fit:cover\""+
										"class=\"zoom\">"+
								"</div>"+
								"<div class=\"time-duration_duration__2iTkq\">"+
									"<div class=\"time-duration_time__2HNys\">"+
										"<span>"+item.video_duration+"</span>"+
									"</div>"+
								"</div>"+
							"</div>"+
						"</a>"+
						"<div class=\"jsx-2348083694 jsx-3258712038 tw-flex tw-justify-between tw-items-stretch tw-flex-col tw-flex-shrink tw-flex-grow-0 tw-min-w-0 tw-h-72 xl:tw-h-88\">"+
							"<div class=\"jsx-2348083694 jsx-3258712038 line-2 tw-text-14 xl:tw-text-16  tw-text-common-black tw-font-semibold\">"+
								"<a target=\"_self\" href='http://127.0.0.1:8899/html/car_video_broadcast.html?"+item.video_id+"' class=\"jsx-2348083694 jsx-3258712038\" rel=\"opener\">"+item.video_introduction+"</a>"+
							"</div>"+
							"<div class=\"jsx-2348083694 jsx-3258712038 tw-whitespace-nowrap tw-flex tw-min-w-0 tw-justify-between tw-text-color-gray-800\">"+
								"<a href='http://127.0.0.1:8899/html/car_my_homepage.html?"+item.user.user_id+"' target=\"_blank\""+
									"class=\"jsx-2348083694 jsx-3258712038 md:tw-inline-block tw-hidden hover:tw-text-common-orange tw-text-14 tw-text-video-shallow-gray tw-mr-16 tw-flex-shrink tw-flex-grow-0 tw-min-w-0  line-1\""+
									"rel=\"opener\">"+item.user.user_name+"</a>"+
								"<span class=\"jsx-2348083694 jsx-3258712038 tw-text-14 tw-inline-block tw-flex-shrink-0 tw-flex-grow-0 tw-text-video-shallow-gray\">"+
									"<span class=\"jsx-2348083694 jsx-3258712038\">"+item.video_broadcast_count+" <!-- -->次播放</span>"+
								"</span>"+
							"</div>"+	
						"</div>"+	
					"</div>"+
				"</li>"
				}
		});
		$("#video_tui_jiang li").remove();
		$("#video_tui_jiang").append(str);
	},"json");
}

function tite_li(car_series_id){
	//登录
	$("#login_button>a").attr("href","http://127.0.0.1:8899/html/car_login.html?car_video_broadcast/"+car_series_id+"");
}

/* 关注/取消关注 */
function insert_or_delete_follow_user(cover_user_id) {
	if (user_id == 0) { //未登录
		location.href = "http://127.0.0.1:8899/html/car_login.html?car_video_broadcast/"+car_series_id;
		return;
	}else{
		var type = $("#followed").css("display") == "none" ? "insert" : "delete";
		$.ajax({
			url: "http://127.0.0.1:8899/api/consumer/Follow_userController/insert_or_delete_follow_user_id",
			type: "post",
			data: JSON.stringify({
				"follow_user": {
					"follow_user_fk_id": user_id,
					"follow_cover_user_fk_id": cover_user_id
				},
				"type": type
			}),
			contentType: 'application/json;charset=utf-8',
			dataType: "json",
			success: function(data) {
				if (data.result == "true") {
					if (type == "insert") {
						$("#follow").hide();
						$("#followed").show();
					} else if(type=="delete"){
						$("#follow").show();
						$("#followed").hide();
					}
				}
			}
		});
	}
}

/* 查询用户是否已关注该用户 */
function select_user_whether_follow_user(cover_user_id) {
	if(user_id==cover_user_id){
		$("#follow").hide();
		$("#followed").hide();
	}else{
		$.ajax({
			url: "http://127.0.0.1:8899/api/consumer/Follow_userController/select_user_follow_user",
			type: "post",
			data: JSON.stringify({
				"follow_user": {
					"follow_user_fk_id": user_id,
					"follow_cover_user_fk_id": cover_user_id
				}
			}),
			contentType: 'application/json;charset=utf-8',
			dataType: "json",
			success: function(data) {
				if (data.result == "true") {
					$("#follow").hide();
					$("#followed").show();
				}
			}
		});
	}
}

//收藏视频
function shochan(){
	if (user_id == 0) { //未登录
		location.href = "http://127.0.0.1:8899/html/car_login.html?car_video_broadcast/"+car_series_id;
		return;
	}else{
		var type = $("#bzs").css("display") == "none" ? "insert" : "delete";
		$.ajax({
			url: "http://127.0.0.1:8899/api/consumer/collection_videoController/insert_or_delete_collection_video/",
			type: "post",
			data: JSON.stringify({
				"collection_video": {
					"collection_video_user_fk_id": user_id,
					"collection_video_fk_id": car_series_id //视频id
				},
				"type": type
			}),
			contentType: 'application/json;charset=utf-8',
			dataType: "JSON",
			success: function(data) {
				if (data.result == "true") {
					if (type == "insert") {
						$("#zs").hide();
						$("#bzs").show();
					} else if (type == "delete") {
						$("#bzs").hide();
						$("#zs").show();
					}
				}
			}
		});
	}
}

//是否收藏  
function isno(){
	$.ajax({
		url: "http://127.0.0.1:8899/api/consumer/collection_videoController/select_voider_broadcast",
		type: "post",
		data: JSON.stringify({
			"collection_video": {
				"collection_video_user_fk_id": user_id,
				"collection_video_fk_id": car_series_id
			}
		}),
		contentType: 'application/json;charset=utf-8',
		dataType: "json",
		success: function(data) {
			if (data.result == "true") {
				$("#zs").hide();
				$("#bzs").show();
			}
		}
	});
}